<?php

namespace app\admin\controller;

use app\lib\enum\RecommendStatus;
use think\Exception;
use think\Request;

class Recommend extends BaseController
{
    //轮播图列表
    public function CarouselList(){
        if(request()->isAjax()){
            $condition=[];
            $condition['rec_role']=['eq',RecommendStatus::Carousel];
            $data=model("PyRecommend")->where($condition)->select();
            foreach ($data as &$value){
                $value['rec_create_time']=date("Y-m-d H:i:s",$value['rec_create_time']);
            }
            $count=count($data);
            return getJson($count,$data);
        }
        $this->assign("rec_role",RecommendStatus::Carousel);
        return view();
    }

    //平台推广列表
    public function RecommendList(){
        if(request()->isAjax()){
            $condition=[];
            $condition['rec_role']=['eq',RecommendStatus::Recommend];
            $data=model("PyRecommend")->where($condition)->select();
            foreach ($data as &$value){
                $value['rec_create_time']=date("Y-m-d H:i:s",$value['rec_create_time']);
            }
            $count=count($data);
            return getJson($count,$data);
        }
        $this->assign("rec_role",RecommendStatus::Recommend);
        return view();
    }

    //添加
    public function dataAdd(){
        $option=input("get.option");
        if(request()->isAjax()){
            $data=Request::instance()->param();
            $data['rec_create_time']=time();
            if($data['rec_order']==""){
                $data['rec_order']=2;
            }
            if(!in_array($data['rec_role'],[RecommendStatus::Carousel,RecommendStatus::Recommend])){
                $this->error("非法参数");
            }
            if($data['rec_image']==""){
                $this->error("请上传图片");
            }
            $this->uniteResponse(model("PyRecommend")->add($data),"数据添加成功");
        }
        if($option=="carousel"){
            $rec_role=RecommendStatus::Carousel;
        }else if($option=="recommend"){
            $rec_role=RecommendStatus::Recommend;
        }
        $this->assign("rec_role",$rec_role);
        return view();
    }
    //处理图片上传
    public function uploadImg(){
        $file = request()->file('file');
        $src=imgUpload($file,false);
        $result=[
            "code"=>0,
            "mag"=>"上传成功",
            "data"=>[
                "src"=>$src,
            ]
        ];
        return json($result);
    }

    //显示大图
    public function showImage(){
        $url=input("get.url");
        $this->assign("url",$url);
        $title=input("get.title");
        $this->assign("title",$title);
        return view();
    }

    //信息编辑
    public function dataEdit(){
        $rec_id=input("get.rec_id");
        if(request()->isAjax()){
            $data=Request::instance()->param();
            $this->uniteResponse(model("PyRecommend")->edit($data,$rec_id),"信息编辑成功");
        }
        $info=model("PyRecommend")->find($rec_id);
        $this->assign("info",$info);
        return view();
    }

    //信息删除
    public function dataDel(){
        $rec_id=input("get.rec_id");
        try{
            $result=model("PyRecommend")->where("rec_id","=",$rec_id)->delete();
        }catch (Exception $exception){
            $this->error("删除异常");
        }
        $this->success("删除成功");
    }
}
